package com.mi.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.mi.common.core.util.Result;
import com.mi.common.core.validation.Save;
import com.mi.common.core.validation.Update;
import com.mi.common.log.annotation.Log;
import com.mi.common.security.annotation.AnonymousAccess;
import com.mi.system.dto.query.UserQueryDTO;
import com.mi.system.dto.request.UserEnableRequestDTO;
import com.mi.system.dto.request.UserLoginRequestDTO;
import com.mi.system.dto.request.UserModifyPasswordRequestDTO;
import com.mi.system.dto.request.UserRequestDTO;
import com.mi.system.entity.User;
import com.mi.system.service.UserService;
import com.mi.system.vo.UserAuthVO;
import com.mi.system.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.mi.common.core.util.Result.ok;
import static com.mi.common.log.constant.LogConstants.SERVICE_ID_USER;

/**
 * User controller
 *
 * @author jin_huaquan
 * @version 1.0
 * @date 2021/11/1 15:10
 */
@Api(value = "用户API", tags = "User")
@ApiSort(999)
@RestController
@RequestMapping("/api/v1/users")
@RequiredArgsConstructor
@Slf4j
public class UserController {

    private final UserService userService;

    @ApiOperation(value = "分页查询用户列表", notes = "分页查询用户列表")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 1)
    @Log(title = "查询用户列表", serviceId = SERVICE_ID_USER)
    @GetMapping("/actions/list")
    public Result<IPage<UserVO>> listUser(Page page, UserQueryDTO query) {
        log.info("API CALL : list user.");

        return ok(userService.listUser(page, query));
    }

    @ApiOperation(value = "罗列用户", notes = "罗列用户")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 2)
    @Log(title = "罗列用户", serviceId = SERVICE_ID_USER)
    @GetMapping("/actions/query")
    public Result<List<UserVO>> listUser(UserQueryDTO query) {
        log.info("API CALL : list user.");

        return ok(userService.listUser(query));
    }

    @ApiOperation(value = "查询当前用户信息", notes = "查询当前用户信息")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 3)
    @Log(title = "查看个人信息", serviceId = SERVICE_ID_USER)
    @GetMapping("/current")
    public Result<UserVO> getCurrentUser() {
        log.info("API CALL : get current user info.");

        return ok(userService.getCurrentUser());
    }

    @ApiOperation(value = "用户登录", notes = "用户登录")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 4)
    @Log(title = "登录系统", serviceId = SERVICE_ID_USER)
    @PostMapping("/actions/login")
    @AnonymousAccess
    public Result<UserAuthVO> login(@Valid @RequestBody UserLoginRequestDTO request) {
        log.info("API CALL : user login.");

        return ok(userService.login(request));
    }

    @ApiOperation(value = "保存用户", notes = "保存用户")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 5)
    @Log(title = "新建用户:#{#request.username}", serviceId = SERVICE_ID_USER)
    @PostMapping
    public Result<Boolean> saveUser(@Validated({Save.class}) @RequestBody UserRequestDTO request) {
        log.debug("API CALL : save user.");

        return ok(userService.saveUser(request));
    }

    @ApiOperation(value = "更新用户", notes = "更新用户信息")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 6)
    @ApiImplicitParam(name = "id", value = "用户的ID", paramType = "path", dataType = "java.lang.String", required = true)
    @Log(title = "编辑用户:#{#request.username}, serviceId = SERVICE_ID_USER")
    @PutMapping("/{id}")
    public Result<Boolean> updateUser(@PathVariable Long id, @Validated({Update.class}) @RequestBody UserRequestDTO request) {
        log.debug("API CALL : update user.");

        return ok(userService.updateUser(id, request));
    }

    @ApiOperation(value = "用户修改账号的密码", notes = "用户修改账号的密码")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 8)
    @Log(title = "修改账号的密码, serviceId = SERVICE_ID_USER")
    @PutMapping("/password/actions/modify")
    public Result<Boolean> modifyPassword(@Validated({Update.class}) @RequestBody UserModifyPasswordRequestDTO request) {
        log.debug("API CALL : modify password.");

        return ok(userService.modifyPassword(request));
    }

    @ApiOperation(value = "管理员修改用户的密码", notes = "管理员修改用户的密码")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 7)
    @ApiImplicitParam(name = "id", value = "用户的ID", paramType = "path", dataType = "java.lang.String", required = true)
    @Log(title = "修改用户#{#request.username}的密码, serviceId = SERVICE_ID_USER")
    @PutMapping("/{id}/password/actions/modify")
    public Result<Boolean> modifyPassword(@PathVariable Long id,
                                          @Validated({Save.class}) @RequestBody UserModifyPasswordRequestDTO request) {
        log.debug("API CALL : admin modify user's password.");

        return ok(userService.modifyPassword(id, request));
    }

    @ApiOperation(value = "启用/禁用用户", notes = "启用/禁用用户")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 8)
    @ApiImplicitParam(name = "id", value = "用户的ID", paramType = "path", dataType = "java.lang.String", required = true)
    @Log(title = "#{#request.enabled == 1 ? '启用':'禁用'}用户:#{#request.username}", serviceId = SERVICE_ID_USER)
    @PutMapping("/{id}/actions/enable")
    public Result<Boolean> enableUser(@PathVariable Long id,
                                      @Valid @RequestBody UserEnableRequestDTO request) {
        log.debug("API CALL : enable user.");

        return ok(userService.enableUser(id, request));
    }

    @ApiOperation(value = "删除用户", notes = "删除用户")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 9)
    @ApiImplicitParam(name = "id", value = "用户的ID", paramType = "path", dataType = "java.lang.String", required = true)
    @Log(title = "删除用户:#{#request.username}", serviceId = SERVICE_ID_USER)
    @DeleteMapping("/{id}")
    public Result<User> removeUser(@PathVariable Long id) {
        log.debug("API CALL : remove user.");

        return ok(userService.removeUser(id));
    }
}
